package leetcode;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;

/**
 * ClassName: BaiduT1
 * Package: leetcode
 * Description:
 *
 * @Author wjn
 * @Create 2025/9/2 19:19
 * @Version 1.0
 */
public class HuaweiT1 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int k = in.nextInt();
        int n = in.nextInt();
        int[] scores = new int[n];
        for(int i = 0; i < n; i++){
            scores[i] = in.nextInt();
        }

        if(n == 0){
            System.out.println(0);
            return;
        }

        long[] dp = new long[n];
        Deque<Integer> deque = new ArrayDeque<>();

        dp[0] = scores[0];
        deque.offerLast(0);

        for (int i = 1; i < n; i++) {
            while (!deque.isEmpty() &&  deque.peekFirst() < i - k) {
                deque.pollFirst();
            }

            if (!deque.isEmpty()) {
                dp[i] = scores[i] + dp[deque.peekFirst()];
            } else {
                dp[i] = scores[i];
            }
            while (!deque.isEmpty() && dp[deque.peekLast()] <= dp[i]) {
                deque.pollLast();
            }
            deque.offerLast(i);
        }
        System.out.println(dp[n - 1]);
        in.close();
    }
}
